package com.mini.example;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.ParameterizedPreparedStatementSetter;

import com.mini.core.dao.MiniDao1;
import com.mini.core.dynamic.DatabaseContextHolder;
import com.mini.core.grammar.Record;
import com.mini.example.bean.User;

public class ExampleMini1 {

    static ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

    static MiniDao1 dao = (MiniDao1) ctx.getBean("miniDao");

    Logger logger = LoggerFactory.getLogger(ExampleMini1.class);

    /**
     * 测试插入实例
     */
    public static int testInsert() {
        User user = new User();
        user.setCreateTime(new Date())
        .setName("username")
        .setType(new Random().nextInt(5));
        int result = dao.insert(user);
        return result;
    }

    /**
     * 测试插入记录
     */
    public static int testInsert2() {
        Record record = new Record("user","id");
        record.set("id", new Random().nextInt(100))
        .set("name", "username")
        .set("type", new Random().nextInt(5))
        .set("password", "000000");
        int result = dao.insert(record);
        return result;
    }

    /**
     * 无参执行sql
     */
    public static int testExecute() {
        int result = 0;
        String sql = "update user set password='2' where id=2";
        result = dao.execute(sql);
        return result;
    }

    /**
     * 有参执行sql
     */
    public static int testExecute2() {
        int result = 0;
        String sql = "update user set password='wefw' where id=? and name=?";
        result = dao.execute(sql, 4, "wef");
        return result;
    }

    /**
     * 批量执行cud
     */
    public static int[] testBatch() {
        int[] result = null;
        result = dao.executebatch(new String[] {"update user set password='gegwewgewg' where id=4",
                "update user set password='wfwfe' where id>8" });
        return result;
    }

    /**
     * 批量执行同一cud操作
     */
    public static int[][] testBatch2() {
        int[][] result = null;
        List<Object> list = new ArrayList<Object>();
        for (int i = 3; i < 7; i++) {
            User usero = new User();
            usero.setPassword(String.valueOf(i));
            list.add(usero);
        }
        result = dao.executebatch("update user set password=? where id=?", list, list.size(),
                new ParameterizedPreparedStatementSetter<Object>() {
                    public void setValues(PreparedStatement ps, Object argument) throws SQLException {
                        ps.setString(2, ((User) argument).getId().toString());
                        ps.setString(1, ((User) argument).getPassword());
                    }
                });
        return result;
    }

    /**
     * 无参查询字符串
     * 
     * @return
     */
    public static String testQueryString() {
        String result = null;
        result = dao.queryString("select name from user where id=10");
        return result;
    }

    /**
     * 有参查询字符串
     * 
     * @return
     */
    public static String testQueryString2() {
        String result = null;
        result = dao.queryString("select name from user where id=?", 23);
        return result;
    }

    /**
     * 无参查询数值
     * 
     * @return
     */
    public static int testQueryInt() {
        int result = 0;
        result = dao.queryInt("select count(id) from user");
        return result;
    }

    /**
     * 有参查询数值
     * 
     * @return
     */
    public static int testQueryInt2() {
        int result = 0;
        result = dao.queryInt("select count(id) from user where id<?", 50);
        return result;
    }

    /**
     * 根据id获取实例
     * 
     * @return
     */
    public static User getObject1() {
        User result = null;
        result = dao.findById(User.class, 22);
        return result;
    }

    /**
     * 无参获取实例
     * 
     * @return
     */
    public static User getObject2() {
        User result = null;
        result = dao.find("select * from user where id=4", User.class);
        return result;
    }

    /**
     * 有参获取实例
     * 
     * @return
     */
    public static User getObject3() {
        User result = null;
        result = dao.find("select * from user where id=? and name=?", User.class, 2, "helelo");
        return result;
    }

    /**
     * 无参获取记录
     * 
     * @return
     */
    public static Record getRecord() {
        Record result = null;
        result = dao.findRecord("select name,type from user where id=3");
        return result;
    }

    /**
     * 有参获取记录
     * 
     * @return
     */
    public static Record getRecord2() {
        Record result = null;
        result = dao.findRecord("select name,type from user where id =?", 3);
        return result;
    }

    /**
     * 无参获取实例列表
     * 
     * @return
     */
    public static List<User> getList() {
        List<User> result = null;
        result = dao.findList("select * from user where id<99", User.class);
        return result;
    }

    /**
     * 有参获取实例列表
     * 
     * @return
     */
    public static List<User> getList2() {
        List<User> result = null;
        result = dao.findList("select * from user where id <? and password like ?", User.class, 5, "%'");
        return result;
    }

    /**
     * 无参获取记录列表
     * 
     * @return
     */
    public static List<Record> getRecordList() {
        List<Record> result = null;
        result = dao.findRecordList("select name,type from user where id<777");
        return result;
    }

    /**
     * 有参获取记录列表
     * 
     * @return
     */
    public static List<Record> getRecordList2() {
        List<Record> result = null;
        result = dao.findRecordList("select name ,type from user where id <? and name like ?", 57, "user");
        return result;
    }

    /**
     * 更新实例
     * 
     * @return
     */
    public static int testUpdate() {
        int result = 0;
        User user = new User();
        user.setName("hello_update");
        result = dao.update(user);
        return result;
    }

    /**
     * 更新记录
     * 
     * @return
     */
    public static int testUpdateRecord() {
        int result = 0;
        Record record = new Record();
        record.setTableName("user");
        record.setPrimaryKeys("id");
        record.set("id", "8");
        record.set("name", "record_update");
        result = dao.update(record);
        return result;
    }

    /**
     * 删除实体
     * @return
     */
    public static int testDelete() {
        int result = 0;
        User user = new User();
        result = dao.delete(user);
        return result;
    }

    /**
     * 删除记录
     * @return
     */
    public static int testDeleteRecord() {
        int result = 0;
        Record record = new Record();
        record.setTableName("user");
        record.setPrimaryKeys("id");
        record.set("id", "8");
        result = dao.delete(record);
        return result;
    }

    /**
     * 无参分页
     */
    public static List<User> testGetPage() {
        List<User> result = null;
        String sql = "select * from user where id < 8 order by id desc";
        result = dao.paginate(2, 2, User.class, sql);
        return result;
    }

    /**
     * 有参分页
     */
    public static List<User> testGetPage2() {
        List<User> result = null;
        String sql = "select * from user where id < ? order by id desc";
        result = dao.paginate(2, 2, User.class, sql, 18);
        return result;
    }

    /**
     * 无参非所有列分页
     */
    public static List<Record> testGetPage3() {
        String sql = "select id,name from user where id < 8 order by id desc";
        return dao.paginate(2, 2, sql);
    }

    /**
     * 有参非所有列分页
     * 
     */
    public static List<Record> testGetPage4() {
        String sql = "select id,name from user where id < ? order by id desc";
        return dao.paginate(2, 2, sql, 88);
    }

    static class MyThread1 extends Thread {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            testInsert();
        }
    }

    static class MyThread2 extends Thread {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            DatabaseContextHolder.setCustomerType("key2");
            testInsert2();
        }
    }

    public static void main(String[] args) {
        // TODO
        testInsert();
       /* testInsert2();
        testExecute();
        testExecute2();
        testBatch();
        testBatch2();
        testQueryString();
        testQueryString2();
        testQueryInt();
        testQueryInt2();
        getObject1();
        getObject2();
        getObject3();
        getRecord();
        getRecord2();
        getList();
        getList2();
        getRecordList();
        getRecordList2();
        testUpdate();
        testUpdateRecord();
        testDelete();
        testDeleteRecord();
        testGetPage();
        testGetPage2();
        testGetPage3();
        testGetPage4();*/
        /*DatabaseContextHolder.setCustomerType("key2");
        testInsert();*/
       /* MyThread2 t1 = new MyThread2();
        MyThread1 t2 = new MyThread1();
        t1.start();
        t2.start();*/
    }
}
